package Main;

import model.CourseSystem;
import model.Student;
import model.Teacher;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        CourseSystem system = new CourseSystem();
        Scanner scanner = new Scanner(System.in);

        System.out.println("===== 学生选课系统 =====");
        // 选择身份
        System.out.println("请选择身份：");
        System.out.println("1. 学生");
        System.out.println("2. 老师");
        System.out.print("请输入（1-2）：");

        int roleChoice;
        try {
            roleChoice = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("输入错误，程序退出！");
            scanner.close();
            return;
        }

        // 学生身份处理
        if (roleChoice == 1) {
            System.out.print("请输入你的学生ID（示例：S001）：");
            String studentId = scanner.nextLine();
            Student student = system.findStudent(studentId);
            if (student == null) {
                System.out.println("错误：学生ID不存在！");
                scanner.close();
                return;
            }
            System.out.println("登录成功！欢迎你，" + student.getName() + "（" + student.getGender() + "）！\n");

            // 学生功能菜单
            while (true) {
                System.out.println("===== 学生功能菜单 =====");
                System.out.println("1. 查看所有课程（含老师信息）");
                System.out.println("2. 选课");
                System.out.println("3. 退课");
                System.out.println("4. 查看我的课表");
                System.out.println("5. 退出系统");
                System.out.print("请选择功能（1-5）：");

                int choice = getValidChoice(scanner);
                if (choice == -1) continue;

                switch (choice) {
                    case 1:
                        system.showAllCourses();
                        break;
                    case 2:
                        System.out.print("请输入要选的课程号（例如C001）：");
                        String selectId = scanner.nextLine();
                        boolean selectSuccess = system.selectCourse(student, selectId);
                        System.out.println(selectSuccess ? "选课成功！" : "选课失败（可能已选该课程）！");
                        break;
                    case 3:
                        System.out.print("请输入要退的课程号（例如C001）：");
                        String dropId = scanner.nextLine();
                        boolean dropSuccess = system.dropCourse(student, dropId);
                        System.out.println(dropSuccess ? "退课成功！" : "退课失败（可能未选该课程）！");
                        break;
                    case 4:
                        system.showStudentSchedule(student);
                        break;
                    case 5:
                        System.out.println("感谢使用，再见！");
                        scanner.close();
                        return;
                    default:
                        System.out.println("无效选项，请输入1-5！");
                }
                System.out.println();
            }
        }
        // 老师身份处理（新增）
        else if (roleChoice == 2) {
            System.out.print("请输入你的老师ID（示例：T001）：");
            String teacherId = scanner.nextLine();
            Teacher teacher = system.findTeacher(teacherId);
            if (teacher == null) {
                System.out.println("错误：老师ID不存在！");
                scanner.close();
                return;
            }
            System.out.println("登录成功！欢迎你，" + teacher.getName() + "（" + teacher.getGender() + "）！\n");

            // 老师功能菜单
            while (true) {
                System.out.println("===== 老师功能菜单 =====");
                System.out.println("1. 查看我的授课课表");
                System.out.println("2. 退出系统");
                System.out.print("请选择功能（1-2）：");

                int choice = getValidChoice(scanner);
                if (choice == -1) continue;

                switch (choice) {
                    case 1:
                        system.showTeacherSchedule(teacher);
                        break;
                    case 2:
                        System.out.println("感谢使用，再见！");
                        scanner.close();
                        return;
                    default:
                        System.out.println("无效选项，请输入1-2！");
                }
                System.out.println();
            }
        }
        // 无效身份
        else {
            System.out.println("无效身份选择，程序退出！");
            scanner.close();
        }
    }

    // 工具方法：获取有效的数字选择
    private static int getValidChoice(Scanner scanner) {
        try {
            return Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("输入错误，请输入数字！");
            return -1;
        }
    }
}
